<template>
    <view class="forget">
        <view class="content">
            <!-- 主体 -->
            <view class="main">
                <view class="tips">若你忘记了密码，可在此重置新密码。</view>
                <wInput v-model="oldPwd" type="password" maxlength="11" placeholder="请输入旧密码"></wInput>
                <wInput v-model="newPwd" type="password" maxlength="11" placeholder="请输入新密码" isShowPass></wInput>
                <wInput v-model="confirm_password" type="password" maxlength="11" placeholder="再次确认密码" isShowPass>
                </wInput>
            </view>
            <wButton text="重置密码" bgColor="#354E44" :rotate="isRotate" @click.native="startRePass()"></wButton>
        </view>
    </view>
</template>

<script>
    var _this;
    import wInput from '@/components/watch-login/watch-input.vue' //input
    import wButton from '@/components/watch-login/watch-button.vue' //button
    import userApi from "@/api/user/index.js"
    export default {

        data() {
            return {
                oldPwd: "", //旧密码
                newPwd: "", //新密码
                confirm_password: "", // 确认密码
                isRotate: false, //是否加载旋转
            }
        },
        components: {
            wInput,
            wButton
        },
        mounted() {
            _this = this;
        },
        methods: {
            async startRePass() {
                //重置密码
                if (this.isRotate) {
                    //判断是否加载中，避免重复点击请求
                    return false;
                }
                if (this.oldPwd.length < 6 || this.oldPwd.length > 12) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '密码格式不正确，6~12位'
                    });
                    return false;
                }
                if (this.newPwd.length < 6 || this.newPwd.length > 12) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '密码格式不正确，6~12位'
                    });
                    return false;
                }
                if (this.confirm_password.length < 6 || this.confirm_password.length > 12) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '密码格式不正确，6~12位'
                    });
                    return false;
                }

                if (this.oldPwd == this.newPwd) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '旧密码不能跟新密码一样'
                    });
                    return false;
                }
                if (this.confirm_password !== this.newPwd) {
                    uni.showToast({
                        icon: 'none',
                        position: 'bottom',
                        title: '密码不一致'
                    });
                    return false;
                }

                _this.isRotate = true;
                console.log('调用接口前', uni.getStorageSync("token"))
                var send = {
                    oldPwd: this.oldPwd,
                    newPwd: this.newPwd
                }
                var result = await userApi.updatePassword(send)
                console.log(result);
                if (result.success == true) {
                    _this.isRotate = false;
                    uni.showToast({
                        title: result.message
                    });
                    setTimeout(() => {
                        uni.navigateBack()
                    }, 1000)
                }
            }
        },
    };
</script>

<style scoped>
    .content {
        display: flex;
        flex-direction: column;
        justify-content: center;
        /* margin-top: 128upx; */
    }

    /* 主体 */
    .main {
        display: flex;
        flex-direction: column;
        padding-left: 70upx;
        padding-right: 70upx;
    }

    .tips {
        color: #999999;
        font-size: 28upx;
        margin-top: 64upx;
        margin-left: 48upx;
    }
</style>
